package com.offcn.controller;

import com.offcn.entity.User;
import com.offcn.service.UserService;
import org.apache.catalina.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {
    /*  uid uname utype ucomname utel ucity uaddress umail upwd uremark*/
    @Resource
    private UserService userService;
     @RequestMapping(value = "/register")
     public  String  register(@RequestParam(value="uname",required=false) String uname,
                              @RequestParam(value="utype",required=false) String utype,
                              @RequestParam(value="ucomname",required=false) String ucomname,
                              @RequestParam(value="utel",required=false) String utel,
                              @RequestParam(value="ucity",required=false) String ucity,
                              @RequestParam(value="uaddress",required=false) String uaddress,
                              @RequestParam(value="umail",required=false) String umail,
                              @RequestParam(value="upwd",required=false) String upwd,
                              @RequestParam(value="uremark",required=false) String uremark, HttpSession session)
     {
                     System.out.println(utype);
                     session.setAttribute("user",new User());

                int  num =  userService.insertUser( uname,ucomname, Integer.parseInt(utype), ucity,utel,uaddress, umail, upwd, uremark);
                  if(num>0){
                      return "login";
                }

           return "redirect:/toRegister";

     }

    @RequestMapping(value = "doLogin")
     public   String    doLogin(@RequestParam( value = "uname",required = false) String uname,
                                @RequestParam(value = "upwd",required = false) String upwd,HttpSession session){
         User  u= userService.selectByUser(uname,upwd);
        if(u  !=null){
              session.setAttribute("user",u);
              //return "plus5";
             return "redirect:/newsShow";

          }
        return "redirect:errorLogin";
    }
      @RequestMapping(value = "errorLogin")
      public  String errorLogin(){
          return "login";
      }

    // 去注册
    @RequestMapping(value = "toRegister")
    public   String toRegister(){
        return "register";
    }



}
